﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YbxLibrary
{
    public class JsonStringBuilder
    {

        /// <summary>
        /// 生成六位随机数
        /// </summary>
        /// <returns></returns>
        private static String getFourRandom() {
            
            Random random = new Random();
            int randomNumber = random.Next(1000, 9999); 
            return Convert.ToString(randomNumber);
        }

        /// <summary>
        ///  构造入参
        /// </summary>.
        /// <param name="infno">交易编号</param>
        /// <param name="input">数据体</param>
        /// <param name="opter_input">工号</param>
        /// <param name="opter_name_input">姓名</param>
        /// <param name="mdtrtarea_admvs_input">就医地</param>
        /// <param name="insuplc_admdvs_input">参保地</param>
        /// <param name="sign_no_input">交易签到流水号</param>
        /// <returns></returns>
        public static string BuildJsonString(String infno, String input, String opter_input,String opter_name_input, String mdtrtarea_admvs_input, String insuplc_admdvs_input, String sign_no_input)
        {
            string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
            #region 读取配置文件
            IniFileUtils iniFile = new IniFileUtils(currentDirectory+"yb.ini");

            string mdtrtarea_admvs = mdtrtarea_admvs_input=="" ? iniFile.ReadValue("general", "mdtrtarea_admvs"): mdtrtarea_admvs_input;
            string insuplc_admdvs = insuplc_admdvs_input=="" ? iniFile.ReadValue("general", "insuplc_admdvs"): insuplc_admdvs_input;
            string opter = opter_input == "" ? iniFile.ReadValue("general", "opter") : opter_input;
            string opter_name = opter_name_input == "" ? iniFile.ReadValue("general", "opter_name") : opter_name_input;
            string sign_no = sign_no_input == "" ? iniFile.ReadValue("general", "sign_no") : sign_no_input;

            string fixmedins_code = iniFile.ReadValue("general", "fixmedins_code");
            string fixmedins_name = iniFile.ReadValue("general", "fixmedins_name");
            string infver = iniFile.ReadValue("general", "infver");
            string recer_sys_code = iniFile.ReadValue("general", "recer_sys_code");
            string opter_type = iniFile.ReadValue("general", "opter_type");
         
            string app_id = iniFile.ReadValue("general", "app_id");
            string enc_type = iniFile.ReadValue("general", "enc_type");
            string pw_ecToken = iniFile.ReadValue("general", "pw_ecToken");
            string dev_no = iniFile.ReadValue("general", "dev_no");
            string dev_safe_info = iniFile.ReadValue("general", "dev_safe_info");
            string cainfo = iniFile.ReadValue("general", "cainfo");
            string signtype = iniFile.ReadValue("general", "signtype");
            #endregion

            #region 变量参数
            DateTime current_time = DateTime.Now;
            #endregion

            Dictionary<string, string> jsonDict = new Dictionary<string, string>
            {
            { "infno", infno },
            { "msgid", fixmedins_code+current_time.ToString("yyyyMMddHHmmss")+getFourRandom() },
            { "mdtrtarea_admvs", mdtrtarea_admvs },
            { "insuplc_admdvs", insuplc_admdvs },
            { "recer_sys_code", recer_sys_code},
            { "dev_no", dev_no },
            { "dev_safe_info", dev_safe_info },
            { "cainfo", cainfo },
            { "signtype", signtype },
            { "infver", infver },
            { "opter_type", opter_type },
            { "opter", opter},
            { "opter_name", opter_name },
            { "inf_time", current_time.ToString("yyyy-MM-dd HH:mm:ss") },
            { "fixmedins_code", fixmedins_code},
            { "fixmedins_name", fixmedins_name },
            { "sign_no", sign_no},
            { "app_id", app_id },
            { "enc_type", enc_type },
            { "pw_ecToken", pw_ecToken }
        };

            StringBuilder jsonString = new StringBuilder();
            jsonString.Append("{");

            bool first = true;
            foreach (var pair in jsonDict)
            {
                if (!first)
                {
                    jsonString.Append(",");
                }
                //jsonString.Append("\"{pair.Key}\": \"{pair.Value}\"");
                jsonString.Append("\""+pair.Key+"\"");
                jsonString.Append(":");
                jsonString.Append("\"" + pair.Value+"\"" );
                first = false;
            }
            jsonString.Append(",");
            jsonString.Append("\"input\":");
            jsonString.Append(input);
            jsonString.Append("}");

            return jsonString.ToString();
        }
    }
}
